<template>
	<view class="wrap">
		<u-navbar :title="$t('hyjqrkt')" title-width="340"  :border-bottom="false" back-icon-color="#fff" title-color="#fff" :background="{ backgroundColor: '#0030A8' }"></u-navbar>
		<view class="tom-container">
			<view class="list">
				<view class="nr_top">
					<view class="left">
						<image class="left_img" :src="`/static/bizhong/${exchangeCoins[exchangeCoins_index].img}.png`"></image>
						<view class="left_select">
							<view class="text" @tap="Select_Ro">{{ exchangeCoins[exchangeCoins_index].label }}</view>
							<u-icon name="you" custom-prefix="custom-icon" size="20"></u-icon>
						</view>
					</view>
					<view class="right">
						<view class="right_xin" style="margin-bottom: 40upx;">
							<view class="right_xin_left">{{$t('jys')}}</view>
							<view class="right_xin_right">
								<view class="label" @tap="Select_Ro2">{{$t('bian')}}</view>
								<u-icon name="you" custom-prefix="custom-icon" size="20"></u-icon>
							</view>
						</view>
						<view class="right_xin">
							<view class="right_xin_left">{{$t('hylx')}}</view>
							<view class="right_xin_right">
								<view class="label" @tap="Select_Ro3">{{ contractType[contractType_index].label }}</view>
								<u-icon name="you" custom-prefix="custom-icon" size="20"></u-icon>
							</view>
						</view>
					</view>
				</view>
				<view class="nr_item">
					<view class="nr_item_left">{{$t('jycl')}}：</view>
					<view class="nr_item_right">
						<view class="strategy">
							<view class="strategy-item" @tap="switchStrategy(index)" v-for="(item, index) in strategyList" :key="index" :class="{ active: strategy == index }">
								{{ item.label }}
								<u-icon name="wenhao" custom-prefix="custom-icon" size="30" style="margin-left: 16rpx;" @tap="Strategy_tips(index)"></u-icon>
							</view>
						</view>
					</view>
				</view>
				<view class="nr_item">
					<view class="nr_item_left">{{$t('xhfs')}}：</view>
					<view class="nr_item_right">
						<view class="select_for" @tap="Select_Ro4">
							{{ tacticsType[tacticsType_index].label }}
							<u-icon name="you" custom-prefix="custom-icon" size="20"></u-icon>
						</view>
					</view>
				</view>
				<view class="nr_item">
					<view class="nr_item_left">{{$t('kyye')}}：</view>
					<view class="nr_item_right">
						<view class="exchange_Balance">
							{{ pageData.exchangeBalance }}
							<text class="text">USDT</text>
						</view>
					</view>
				</view>
				<view class="nr_item nr_item_border">
					<view class="nr_item_left">{{$t('bzj')}}：</view>
					<view class="nr_item_right">
						<view class="limit">
							<input class="limit-input" type="digit" v-model="form.bond" :placeholder="$t('qsrbzj')" :maxlength="10" @blur="inputBond" />
							<text class="limit-text">USDT</text>
						</view>
					</view>
				</view>
				<!-- <view class="nr_item nr_item_border" style="align-items:flex-start;">
					<view class="nr_item_left">开仓金额：</view>
					<view class="nr_item_right">
						<view class="limit">
							<input class="limit-input" type="digit" v-model="form.OrderAmount" placeholder="请输入开仓金额" maxlength="10" @blur="inputOrderAmount" />
							<text class="limit-text">USDT</text>
						</view>
						<view class="Opening_amount" v-if="Number(form.OrderAmount) > $BigNumber(form.bond || 0).multipliedBy(0.5)">开仓金额超过保证金的50%，无法下单</view>
					</view>
				</view> -->
				<view class="nr_item nr_item_border">
					<view class="nr_item_left">{{$t('zycf')}}：</view>
					<view class="nr_item_right">
						<view class="limit">
							<input class="limit-input" type="digit" v-model="form.stopScale" :maxlength="6" :placeholder="$t('qsrzycf')"/>
							<text class="limit-text">{{ $BigNumber(form.stopScale || 0).multipliedBy(100) }}%</text>
						</view>
					</view>
				</view>
				<view class="nr_item nr_item_border">
					<view class="nr_item_left">{{$t('zyhd')}}：</view>
					<view class="nr_item_right">
						<view class="limit">
							<input class="limit-input" type="digit" v-model="form.stopCallback" :maxlength="6" :placeholder="$t('qsrzyhd')"/>
							<text class="limit-text">{{ $BigNumber(form.stopCallback || 0).multipliedBy(100) }}%</text>
						</view>
					</view>
				</view>
				<view class="nr_item nr_item_border">
					<view class="nr_item_left">{{$t('zsbl')}}：</view>
					<view class="nr_item_right">
						<view class="limit">
							<input class="limit-input" type="digit" v-model="form.stopBack" :maxlength="6" :placeholder="$t('qsrzsbl')" @blur="inputStopBack" />
							<text class="limit-text">{{ $BigNumber(form.stopBack || 0).multipliedBy(100) }}%</text>
						</view>
					</view>
				</view>
				<view class="nr_item nr_item_border" style="align-items:flex-start;">
					<view class="nr_item_left">{{$t('ggbs')}}：</view>
					<view class="nr_item_right">
						<view class="limit">
							<u-number-box
								v-model="form.multiple"
								:class="form.multiple >= 5 ? 'cell-right-numberBox' : ''"
								:min="1"
								:max="pageData.maxLeverage[strategy]"
								color="#0032B7"
								:input-height="40"
								@change="changeInfo"
							></u-number-box>
							<text class="limit-text">
								{{$t('bei')}}(
								<block v-if="this.form.bond != 0">
									{{
										$BigNumber(Number(form.OrderAmount || 0))
											.dividedBy(this.form.bond)
											.multipliedBy(100)
											.toFixed(2)
									}}
								</block>
								<block v-else>0</block>
								%{{$t('cangwei')}})
							</text>
						</view>
						<view class="Opening_amount" v-if="form.multiple >= 5">{{$t('gggg')}}</view>
					</view>
				</view>
				<view class="btn_success" @tap="btn_ok"><text class="text">{{$t('qdkt')}}</text></view>
			</view>
		</view>
		<u-picker v-model="show" mode="selector" @confirm="picker_confirm" :range="exchangeCoins" range-key="label"></u-picker>
		<u-picker v-model="show2" mode="selector" :range="list2" range-key="label"></u-picker>
		<u-picker v-model="show3" mode="selector" :range="contractType" @confirm="picker_confirm3" range-key="label"></u-picker>
		<u-picker v-model="show4" mode="selector" :range="tacticsType" @confirm="picker_confirm4" range-key="label"></u-picker>
		<u-toast ref="uToast" />
		<tom-pwd v-if="showPwd" @cancel="showPwd = false" @confirm="confirm"></tom-pwd>
	</view>
</template>

<script>
export default {
	data() {
		return {
			list2: [
				{
					value: '1',
					label: this.$t('bian')
				}
			],
			exchangeCoins: [
				/*币种*/
				{
					value: '',
					label: '',
					index: '',
					img: 'BTC'
				}
			],
			contractType: [
				/*合约类型*/
				{
					value: '',
					label: ''
				}
			],
			tacticsType: [
				/*循环方式*/
				{
					value: '',
					label: ''
				}
			],
			exchangeCoins_index: 0,
			contractType_index: 0,
			tacticsType_index: 0,
			show: false,
			show2: false,
			show3: false,
			show4: false,
			form: {
				multiple: 1 /*杠杆倍数*/,
				stopBack: '' /*止损比例*/,
				stopScale: '' /*止盈触发*/,
				bond: '' /*保证金*/,
				OrderAmount: '' /*下单金额*/,
				exchange: 0 /*交易所*/,
				stopCallback: '' /*止盈回调*/
			},
			showPwd: false /*密码输入状态*/,
			// 策略选择列表
			strategyList: [{ label: this.$t('wdx'), value: 1 }, { label: this.$t('kjkcx'), value: 2 }],
			// 选中策略索引
			strategy: 0,
			pageData: { maxLeverage: [1, 2] }
		};
	},
	onShow() {
		this.indexData();
	},
	methods: {
		indexData() {
			uni.showLoading({
				mask: true,
				title: this.$t('loading.title')
			});
			this.$api.consoleApi
				.getContractRobotOpenPage()
				.then(res => {
					this.pageData = res.data;
					this.form.bond = res.data.margin;
					this.form.OrderAmount = res.data.orderAmount;
					var arr1 = [];
					var arr2 = [];
					var arr3 = [];
					res.data.exchangeCoins.forEach(function(item, index) {
						let str = item.label;
						let str_index = str.lastIndexOf('/');
						str = str.substring(0, str_index);
						arr1.push({ value: item.value, label: item.label, index: index, img: str });
					});
					res.data.contractType.forEach(function(item) {
						arr2.push({ value: item, label: item });
					});
					res.data.tacticsType.forEach(function(item, index) {
						arr3.push({ value: index, label: item });
					});
					this.exchangeCoins = arr1;
					this.contractType = arr2;
					this.tacticsType = arr3;
					console.log(arr1);
					uni.hideLoading();
				})
				.catch(res => {
					uni.hideLoading();
				});
		},
		Select_Ro(e) {
			this.show = true;
		},
		Select_Ro2(e) {
			this.show2 = true;
		},
		Select_Ro3(e) {
			this.show3 = true;
		},
		Select_Ro4(e) {
			this.show4 = true;
		},
		picker_confirm(e) {
			this.exchangeCoins_index = e[0];
		},
		picker_confirm3(e) {
			this.contractType_index = e[0];
		},
		picker_confirm4(e) {
			this.tacticsType_index = e[0];
		},
		inputBond() {
			/*保证金*/
			if (Number(this.form.bond) > this.pageData.exchangeBalance) {
				if(this.pageData.exchangeBalance == 0){
					this.$nextTick(() => {
							this.form.bond = this.pageData.exchangeBalance;
							this.$utils.toast(this.$t('kyyebzy'));
						});
				}else{
					this.$nextTick(() => {
						this.form.bond = this.pageData.exchangeBalance;
						this.$utils.toast(this.$t('bzjzd')+`${this.pageData.exchangeBalance}`);
					});
				}
			}
			if (Number(this.form.bond) < Number(this.pageData.margin)) {
				this.$nextTick(() => {
					this.form.bond = Number(this.pageData.margin);
					this.$utils.toast(this.$t('bzjezd')+`${this.pageData.margin}`);
				});
			}
		},
		inputOrderAmount() {
			/*下单金额*/
			if (Number(this.form.bond) < Number(this.pageData.orderAmount)) {
				this.$nextTick(() => {
					this.form.bond = Number(this.pageData.orderAmount);
					this.$utils.toast(this.$t('xdjezd')+`${this.pageData.orderAmount}`);
				});
			}
		},
		inputStopBack() {
			/*止损比例*/
			console.log(this.form.stopBack);
			if (Number(this.form.stopBack) > 1) {
				this.$nextTick(() => {
					this.form.stopBack = 1;
					this.$utils.toast(this.$t('zsblzd'));
				});
			}
		},

		switchStrategy(i) {
			/*切换策略*/
			var that = this;
			if (this.strategy == i) return;
			this.strategy = i;
			setTimeout(function(){
				that.form.multiple = 1;
				that.$refs.uToast.show({
					title: this.$t('ggbsycz'),
					type: 'warning',
					duration: 1500,
					icon: false
				});
			},200);
		},

		Strategy_tips(e) {
			/*策略详情*/
			this.$refs.uToast.show({
				title: e == 1 ? this.$t('ggbsg') : this.$t('dggwd'),
				type: 'warning',
				duration: 3000,
				icon: false
			});
		},
		changeInfo({ value }) {
			/*杠杆倍数*/
			console.log(value);
			if (value > this.pageData.maxLeverage[this.strategy]) {
				this.$utils.toast(this.$t('ggbszd')+`${this.pageData.maxLeverage[this.strategy]}`);
			}
		},
		btn_ok() {
			console.log(Number(this.form.stopCallback));
			console.log(Number(this.form.stopScale));
			// if (Number(this.form.OrderAmount) > this.$BigNumber(this.form.bond || 0).multipliedBy(0.5)) return this.$utils.toast(`开仓金额超过保证金的50%，无法下单`);
			if (Number(this.form.stopCallback) > 0 && Number(this.form.stopScale) == 0) return this.$utils.toast(this.$t('zycftssz'));
			if (Number(this.form.stopScale) > 0 && Number(this.form.stopCallback) == 0) return this.$utils.toast(this.$t('zycftssz'));
			if (Number(this.form.stopScale) > 10) return this.$utils.toast(this.$t('qsr10b'));
			if (Number(this.form.stopBack) > 10) return this.$utils.toast(this.$t('qsrq0bl'));
			if (Number(this.form.bond) > Number(this.pageData.exchangeBalance)) return this.$utils.toast(this.$t('kyyebzy'));
			if (Number(this.pageData.exchangeBalance) < this.$BigNumber(Number(this.form.OrderAmount) || 0)) return this.$utils.toast(this.$t('kyyebz'));
			this.showPwd = true;
		},
		confirm(e) {
			this.showPwd = false;
			let params = {};
			uni.showLoading({
				mask: true,
				title: this.$t('zztj')
			});
			if (Number(this.form.stopScale)) {
				params.stopScale = Number(this.form.stopScale);
			}
			if (Number(this.form.stopCallback)) {
				params.stopBack = Number(this.form.stopCallback);
			}
			if (Number(this.form.stopBack)) {
				params.stopLoss = Number(this.form.stopBack);
			}
			/*运行机器人接口*/
			this.$api.consoleApi
				.saveContractRobotOpen({
					transferPassword: e,
					contractType: 'USTANDARD',
					exchangeName: 'BINANCE',
					exchangeCoin: this.exchangeCoins[this.exchangeCoins_index].label,
					margin: this.form.bond,
					orderAmount: this.form.OrderAmount,
					leverage: this.form.multiple,
					tacticsType: this.tacticsType[this.tacticsType_index].label == '单次做单' ? 1 : 2,
					dangerType: this.strategy == 0 ? 'STEADY' : 'RADICAL',
					...params
				})
				.then(res => {
					this.indexData();
					this.$utils.toast(this.$t('czcg'));
					uni.hideLoading();
				})
				.catch(res => {
					uni.hideLoading();
				});
		}
	}
};
</script>

<style lang="scss" scoped>
.tom-container {
	padding-top: 50rpx;
}
.list {
	flex: 1;
	border-radius: 50rpx 50rpx 0rpx 0rpx;
	overflow: auto;
	background-color: #fff;
	.nr_top {
		display: flex;
		align-items: center;
		padding: $nr-padd;
		border: 2rpx solid $page-bg;
		.left {
			min-width: 240rpx;
			text-align: center;
			.left_img {
				width: 80rpx;
				height: 80rpx;
				margin-left: -30rpx;
				margin-bottom: 30rpx;
			}
			.left_select {
				display: flex;
				align-items: center;
				justify-content: center;
				.text {
					font-weight: bold;
					font-size: 28rpx;
					margin-right: 10rpx;
				}
				.u-icon {
					transform: rotate(90deg);
				}
			}
		}
		.right {
			width: 100%;
			margin-left: 50rpx;
			.right_xin {
				display: flex;
				justify-content: space-between;
				.right_xin_left {
					color: $tom-text-color;
					font-size: 28rpx;
					font-weight: bold;
				}
				.right_xin_right {
					color: $tom-text-sub-color2;
					display: flex;
					.label {
						font-size: 28rpx;
						margin-right: 20rpx;
					}
					.u-icon {
						transform: rotate(90deg);
					}
				}
			}
		}
	}
	.nr_item {
		padding: 20rpx $nr-padd;
		border: 2rpx solid $page-bg;
		display: flex;
		align-items: center;
		.nr_item_left {
			white-space: nowrap;
			font-weight: bold;
			font-size: 28rpx;
		}
		.nr_item_right {
			.strategy {
				display: flex;
				.strategy-item {
					color: $tom-text-sub-color;
					font-size: 28rpx;
					margin: 0px 20rpx;
				}
				.active {
					color: $text-color;
				}
				.strategy-item_plus {
					border: 2rpx solid $text-color;
					padding: 6rpx 20rpx;
					margin-top: -8rpx;
				}
			}
			.select_for {
				display: flex;
				align-items: center;
				font-size: 28rpx;
				.u-icon {
					transform: rotate(90deg);
					margin-left: 10rpx;
				}
			}
			.exchange_Balance {
				color: $text-color;
				font-size: 28rpx;
				font-weight: bold;
				.text {
					margin-left: 10rpx;
					color: $tom-text-color;
					font-weight: 400;
				}
			}
			.Opening_amount {
				font-size: 24rpx;
				color: $text-color;
				margin-top: 10rpx;
			}
			.limit {
				display: flex;
				align-items: center;
				&-input {
					width: 240rpx;
					height: 50rpx;
					border: 1px solid $tom-text-sub-color;
					border-radius: 4rpx;
					color: $text-color;
					font-size: 24rpx;
					padding: 0 20rpx;
					box-sizing: border-box;
				}
				&-text {
					margin-left: 20rpx;
					color: $tom-text-color;
					font-size: 24rpx;
				}
				.buyin-plusreduce {
					margin-left: 30rpx;
					width: 104rpx;
					height: 40rpx;
					padding: 0 10rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					border: 1px solid $text-color;
					border-radius: 8rpx;
					.buyin-plusreduce-line {
						width: 1px;
						height: 20rpx;
						background-color: #2a405c;
					}
				}
			}
		}
	}
	.nr_item_border {
		border: none;
	}
	.btn_success {
		width: 240rpx;
		height: 80rpx;
		background-color: $text-color;
		color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 10rpx;
		margin: 50rpx auto;
		.text {
			font-size: 32rpx;
			font-weight: bold;
		}
	}
}
</style>
